package com.cloudptt.wp.ui.address;

import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.LinearLayout;
import android.widget.ListView;

import androidx.annotation.Nullable;
import androidx.fragment.app.Fragment;

import com.cloudptt.api.product.Product;
import com.cloudptt.api.product.ProductConst;
import com.cloudptt.api.product.log.Log;
import com.cloudptt.api.product.operator.AddrOperator;
import com.cloudptt.wp.R;
import com.cloudptt.wp.ScreenManager;
import com.cloudptt.wp.SysApplication;
import com.cloudptt.wp.ui.address.adapter.AddrUserListAdapter;
import com.cloudptt.wp.ui.message.ForwardMsgActivity;
import com.cloudptt.wp.ui.message.SessionActivity;
import com.mcptt.config.model.McIdInfo;
import com.mcptt.db.model.AddrInfo;
import com.mcptt.db.model.SessionInfo;

import java.util.ArrayList;
import java.util.List;


/**
 * @author onestravel
 * @createTime 2019-08-04 11:52
 * @description TODO
 */
public class AddrUserFragment extends Fragment implements IAddrFragmentUpdate,View.OnClickListener, AdapterView.OnItemClickListener{
    private Context ctx = null;
    private boolean isGroup = false;
    private ListView listView;
    private LinearLayout mEmptyLayout;
    private List<AddrInfo> addrList = new ArrayList<AddrInfo>();
    private ArrayList<SessionInfo> sessionList = null;
//    private AddrInfo mcIdInfo = null;
    //    private IAddrUpdate update = null;
    private AddrUserListAdapter adapter;
    private Product product;
    private int curGroupPos = 0;
    private boolean isForwardUi = false;
    public AddrUserFragment()
    {}
    public AddrUserFragment(Context ctx, boolean isGroup,boolean isForwardUi,ArrayList<SessionInfo> sessionList){
        this.ctx = ctx;
        this.isGroup = isGroup;
        this.isForwardUi = isForwardUi;
        this.sessionList = sessionList;
        Log.debug(this.getClass(),"onCreate sessionList = " + sessionList);
    }
    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, Bundle savedInstanceState) {
        View mRootView = inflater.inflate(R.layout.fragment_addr,container,false);
        listView = (ListView) mRootView.findViewById(R.id.addr_list);

        SysApplication app = ((SysApplication) ctx.getApplicationContext());
        this.product = app.getProduct();
        if(product!=null){
            AddrOperator operator = (AddrOperator)product.getOperator(ProductConst.Func_Addr);
            List<AddrInfo> addrList = operator.getAddrInfoList(false);
            Log.debug(this.getClass(), "onCreateView addrList = " + addrList);

            AddrInfo mcIdInfo = null;
            if(product.getMcpttLoginInfo().getMcIdInfo() != null)
            {
                McIdInfo info = product.getMcpttLoginInfo().getMcIdInfo();
                mcIdInfo = info.toAddrInfo();
            }
            adapter = new AddrUserListAdapter(getActivity(), addrList,mcIdInfo, isForwardUi);
            listView.setAdapter(adapter);
            listView.setOnItemClickListener(this);

            mEmptyLayout = (LinearLayout)mRootView.findViewById(R.id.empty_ll);
            mEmptyLayout.setOnClickListener(this);

            if(addrList != null
                    && addrList.size() > 0){

                mEmptyLayout.setVisibility(View.INVISIBLE);
            }else{

                mEmptyLayout.setVisibility(View.VISIBLE);
            }
        }

        return mRootView;
    }

    @Override
    public void onResume() {
        Log.debug(this.getClass(),"onResume");
        super.onResume();
        updateMcId();
    }
    public void updateMcId(){
        if(product == null
                && ctx != null){
            SysApplication app = ((SysApplication) ctx.getApplicationContext());
            this.product = app.getProduct();
        }
        if(product!=null){
            McIdInfo info = product.getMcpttLoginInfo().getMcIdInfo();
            if(info != null
                    && this.adapter != null)
            {
                adapter.setMyLoginInfo(info.toAddrInfo());
            }
        }
    }
    public void updateAddrList(boolean isNotify){
        if(product == null
                && ctx != null){
            SysApplication app = ((SysApplication) ctx.getApplicationContext());
            this.product = app.getProduct();
        }
        if(product!=null){
            AddrOperator operator = (AddrOperator)product.getOperator(ProductConst.Func_Addr);
            List<AddrInfo> list = operator.getAddrInfoList(false);
            Log.debug(this.getClass(), "updateAddrList list = " + list);
            updateData(isNotify,list);
        }
    }

    private void updateData(boolean isNotify,List<AddrInfo> addrList)
    {
        Log.debug(this.getClass(), "updateAddrList isNotify = " + isNotify);
//		handler.sendEmptyMessage(0x101);//通过handler发送一个更新数据的标记

        if(addrList == null){
            return;
        }
        Log.debug(this.getClass(), "updateData addrList = "+addrList);
        this.addrList.clear();
        this.addrList.addAll(addrList);
        if(this.adapter != null)
        {
            this.adapter.refresh(this.addrList);
        }

        if(mEmptyLayout == null){
            return;
        }

        if(this.addrList != null
                && this.addrList.size() > 0){

            mEmptyLayout.setVisibility(View.INVISIBLE);
        }else{
            if(isNotify)
            {
                mEmptyLayout.setVisibility(View.INVISIBLE);
            }
            else {
                mEmptyLayout.setVisibility(View.VISIBLE);
            }
        }
    }
    //
//
//    public void setMyLoginInfo(AddrInfo myInfo){
//        this.mcIdInfo = myInfo;
//        if(adapter !=null){
//            adapter.setMyLoginInfo(myInfo);
//            adapter.notifyDataSetChanged();
//        }
//    }
    @Override
    public void onClick(View view) {

    }

    @Override
    public void onItemClick(AdapterView<?> parent, View view, int position, long id)  {
        final AddrInfo info = (AddrInfo)listView.getAdapter().getItem(position);
        goToMsgActivity(info);
    }
    private void goToMsgActivity(AddrInfo addrInfo)
    {
        Log.debug(this.getClass(), "goToMsgActivity addrInfo = " + addrInfo);
        //		AddrInfo addrInfo = groupInfo.getUserList().get(childPosition);
        Intent intent = new Intent(ctx, SessionActivity.class);
        Log.debug(this.getClass(),"goToMsgActivity isForwardUi = " + isForwardUi + " sessionList = " + sessionList);
        if(isForwardUi)
        {
            intent.putParcelableArrayListExtra(ForwardMsgActivity.KEY,sessionList);
        }
        intent.putExtra(SessionActivity.KEY, addrInfo);
        ctx.startActivity(intent);
    }
}
